package com.vipheyue.bigcompany.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.dd.processbutton.iml.SubmitProcessButton;
import com.lidroid.xutils.util.LogUtils;
import com.rengwuxian.materialedittext.MaterialEditText;
import com.vipheyue.bigcompany.R;
import com.vipheyue.bigcompany.utils.ProgressGenerator;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;

public class GetSMSActivity extends Activity implements ProgressGenerator.OnCompleteListener {
    @Bind(R.id.met_phone)
    MaterialEditText metPhone;
    @Bind(R.id.tv_getSMS)
    TextView tvGetSMS;
    @Bind(R.id.et_codeSMS)
    EditText etCodeSMS;
    @Bind(R.id.btn_Send)
    SubmitProcessButton btnSend;
    @Bind(R.id.tv_Menu_Title)
    TextView tvMenuTitle;
    private ProgressGenerator progressGenerator;
    private String type;

    EventHandler eh = new EventHandler() {
        @Override
        public void afterEvent(int event, int result, Object data) {
            LogUtils.i("event:" + event + "    result:" + result + "    data:" + data.toString());

            switch (event) {
                case SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE:
                    if (result == SMSSDK.RESULT_COMPLETE) {
//                        toast("验证成功");//{phone=15902339978, country=86}
                        String dataStr = data.toString();
                        String commitedPhone = dataStr.substring(7, 18);
                        LogUtils.i("截取的" + commitedPhone);
                        Intent intent = new Intent(GetSMSActivity.this, SetPasswordActivity.class);
                        intent.putExtra("commitedPhone", commitedPhone);
                        intent.putExtra("type", type);
                        startActivity(intent);
                        finish();
                    } else {
                        toast("验证失败");
                    }
                    break;
                case SMSSDK.EVENT_GET_VERIFICATION_CODE:
                    if (result == SMSSDK.RESULT_COMPLETE) {
                        toast("获取验证码成功");
                        //默认的智能验证是开启的,我已经在后台关闭
                    } else {
                        toast("获取验证码失败");
                    }
                    break;
            }
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        ButterKnife.bind(this);
        LogUtils.customTagPrefix = "xUtils";
        LogUtils.allowI = true;
        SMSSDK.registerEventHandler(eh); //注册短信回调
        progressGenerator = new ProgressGenerator(this);
        type = getIntent().getStringExtra("type");
        tvMenuTitle.setText(type);
    }


    @OnClick({R.id.tv_getSMS, R.id.btn_Send,R.id.rl_menu_left})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_getSMS:
                String phone = metPhone.getText().toString();
                SMSSDK.getVerificationCode("86", phone);
                seGetSMSState();
                break;
            case R.id.btn_Send:
                progressGenerator.start(btnSend);
                String SMS = etCodeSMS.getText().toString().trim();
                String userPhone = metPhone.getText().toString();
                SMSSDK.submitVerificationCode("86", userPhone, SMS);
                break;
            case R.id.rl_menu_left:
                finish();
        }
    }
    private void seGetSMSState() {
        tvGetSMS.setClickable(false);
        tvGetSMS.setBackgroundColor(Color.parseColor("#D6D7D7"));
        CountDownTimer cdt = new CountDownTimer(60000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                tvGetSMS.setText(millisUntilFinished / 1000 + "S");
            }

            @Override
            public void onFinish() {
                tvGetSMS.setClickable(true);
                tvGetSMS.setBackgroundColor(Color.parseColor("#DFC52A"));
            }
        };
        cdt.start();
    }

    /***********************************************************************************/
    @Override
    public void onComplete() {
        btnSend.setEnabled(true);
        btnSend.setText("下一步");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        SMSSDK.unregisterAllEventHandler();
    }

    private void toast(final String str) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(GetSMSActivity.this, str, Toast.LENGTH_SHORT).show();
            }
        });
    }


}